package com.chlp.core.framework.codeGenerator.util;

import com.chlp.core.framework.codeGenerator.entity.BasisInfo;
import com.chlp.core.framework.codeGenerator.entity.PropertyInfo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright: Copyright (c) 2019
 *
 * <p>说明：  链接数据库并获取表信息</P>
 * version: v3.0.0
 *
 * @author: flying-cattle
 * <p>
 * Modification History:
 * Date         	Author          Version          Description
 * ---------------------------------------------------------------*
 * 2019年4月9日      		flying-cattle   v3.0.0           initialize
 */
public class EntityInfoUtil {

	public static void getInfo(BasisInfo bi) {
		List<PropertyInfo> columns = new ArrayList<>();
		// 创建连接
		Connection con = null;
		PreparedStatement pstemt = null;
		ResultSet rs = null;
		//sql
		StringBuilder sql = new StringBuilder();
		sql.append("select column_name,data_type,column_comment,COLUMN_KEY from information_schema.columns where table_schema='");
		sql.append(bi.getDatabase());
		sql.append("' and table_name='");
		sql.append(bi.getTable());
		sql.append("'");

		StringBuilder sql1 = new StringBuilder();
		sql1.append("SHOW TABLE STATUS FROM ").append(bi.getDatabase()).append(" WHERE NAME = '");
		sql1.append(bi.getTable());
		sql1.append("';");

		try {
			con = DriverManager.getConnection(bi.getDbUrl(), bi.getDbName(), bi.getDbPassword());
			pstemt = con.prepareStatement(sql.toString());
			rs = pstemt.executeQuery();

			Connection con1 = DriverManager.getConnection(bi.getDbUrl(), bi.getDbName(), bi.getDbPassword());
			PreparedStatement pstemt1 = con1.prepareStatement(sql1.toString());
			ResultSet rs1 = pstemt1.executeQuery();
			while (rs1.next()) {
				String entityComment = rs1.getString(18);
				bi.setEntityComment(entityComment);
			}

			while (rs.next()) {
				String column = rs.getString(1);
				String jdbcType = rs.getString(2);
				String comment = rs.getString(3);
				String priKey = rs.getString(4);
				PropertyInfo ci = new PropertyInfo();
				if (priKey != null && !"".equals(priKey) && "PRI".equals(priKey)) {
					ci.setPriKey("pri");
				}else {
					ci.setPriKey("other");
				}
				ci.setColumn(column);
				if ("int".equalsIgnoreCase(jdbcType)) {
					ci.setJdbcType("Integer");
				} else if ("datetime".equalsIgnoreCase(jdbcType)) {
					ci.setJdbcType("timestamp");
				} else {
					ci.setJdbcType(jdbcType);
				}
				ci.setComment(comment);
				ci.setProperty(MySqlToJavaUtil.changeToJavaFiled(column));
				ci.setJavaType(MySqlToJavaUtil.jdbcTypeToJavaType(jdbcType));
				//设置注解类型
				if ("id".equalsIgnoreCase(column)) {
					bi.setIdType(ci.getJavaType());
					bi.setIdJdbcType(ci.getJdbcType());
				}
				columns.add(ci);
			}
			bi.setCis(columns);
			// 完成后关闭
			rs.close();
			pstemt.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("自动生成实体类错误：" + e.getMessage());
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstemt != null) {
					pstemt.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException se2) {
				se2.printStackTrace();
			}
		}
	}
}
